package cn.smbms.config;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

public class WebConfigInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext container) {
		//初始化 spring ioc
		initializeSpringConfig(container);
		//初始化 spring mvc
		initializeSpringMVCConfig(container);
		//初始化过滤器
		registerFilter(container);
		//registerServlet(container);
		//registerListener(container);
	}

	private void initializeSpringConfig(ServletContext container) {
		// Create the 'root' Spring application context
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(ApplicationConfig.class);
		// Manage the life cycle of the root application context
		container.addListener(new ContextLoaderListener(rootContext));
	}

	private void initializeSpringMVCConfig(ServletContext container) {
		// Create the spring rest servlet's Spring application context
		AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
		dispatcherContext.register(SpringMvcConfig.class);
		// Register and map the spring rest servlet
		ServletRegistration.Dynamic dispatcher = container.addServlet("SpringMvc",
				new DispatcherServlet(dispatcherContext));
		dispatcher.setLoadOnStartup(2);
		dispatcher.setAsyncSupported(true);
		dispatcher.addMapping("/");
	}

	private void registerFilter(ServletContext container) {
		initializeCharacterFilter(container);
		initializeShiroFilter(container);
	}

	private void initializeCharacterFilter(ServletContext container) {
		CharacterEncodingFilter characterFilter = new CharacterEncodingFilter();
		characterFilter.setEncoding("UTF-8");
		characterFilter.setForceEncoding(true);
		FilterRegistration.Dynamic encodingFilter = container.addFilter("encodingFilter", characterFilter);
		encodingFilter.addMappingForUrlPatterns(null,false,"/*");
	}

	private void initializeShiroFilter(ServletContext container) {
		FilterRegistration.Dynamic filterRegistration = container.addFilter("shiroFilter", DelegatingFilterProxy.class);
		filterRegistration.addMappingForUrlPatterns(null, false, "/*");
		filterRegistration.setInitParameter("targetFilterLifecycle", "true");
		filterRegistration.setInitParameter("targetBeanName", "shiroFilter");
		filterRegistration.setAsyncSupported(true);
	}
	/*

	private void registerServlet(ServletContext container) {
		initializeStaggingServlet(container);
	}

	private void registerListener(ServletContext container) {
		container.addListener(RequestContextListener.class);
	}

	private void initializeStaggingServlet(ServletContext container) {
		StaggingServlet staggingServlet = new StaggingServlet();
		ServletRegistration.Dynamic dynamic = container.addServlet("staggingServlet", staggingServlet);
		dynamic.setLoadOnStartup(3);
		dynamic.addMapping("*.stagging");
	}
	*/
}